﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace QidorSpider.Components.Util
{
    public class ExportHelper
    {
        ContainerControl m_Sender = null;
        int m_Total = 0;
        Delegate m_SenderDelegate = null;
        Common.PostType m_PostType = Common.PostType.NoPost;
        string m_FileName = "";
        DataTable m_PostData;

        #region ExportHelper
        public ExportHelper()
        {
        }

        ~ExportHelper()
        {
        }

        public ExportHelper(ContainerControl sender, Delegate senderDelegate, Common.PostType postType, string fileName, DataTable postData)
        {
            m_Sender = sender;
            m_SenderDelegate = senderDelegate;
            m_PostType = postType;
            m_FileName = fileName;
            m_PostData = postData;
        }
        #endregion

        #region RunProcess
        public void RunProcess(object obj)
        {
            Thread.CurrentThread.IsBackground = true; //make them a daemon
            object[] objArray = (object[])obj;
            m_Sender = (System.Windows.Forms.Form)objArray[0];
            m_SenderDelegate = (System.Delegate)objArray[1];
            m_Total = (int)objArray[2];

            LocalRunProcess();
        }

        public void RunProcess()
        {
            Thread.CurrentThread.IsBackground = true; //make them a daemon
            LocalRunProcess();
        }

        private void LocalRunProcess()
        {
            switch(m_PostType)
            {
                case Common.PostType.PostAccess:
                    break;
                case Common.PostType.PostExcel:
                    ExportExcel();
                    break;
                case Common.PostType.PostText:
                    ExportText();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region ExportExcel
        /// <summary>
        /// export to excel
        /// </summary>
        public void ExportExcel()
        {
            FileStream myStream = File.Open(m_FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            string str = "";
            string tempStr = "";
            int i = 0;
            int Count = 0;

            try
            {
                for(i = 0; i < m_PostData.Columns.Count; i++)
                {
                    str += "\t";
                    str += m_PostData.Columns[i].ColumnName;
                }

                sw.WriteLine(str);
                Count = m_PostData.Rows.Count;
                for(i = 0; i < m_PostData.Rows.Count; i++)
                {
                    for(int j = 0; j < m_PostData.Columns.Count; j++)
                    {

                        tempStr += "\t";
                        tempStr += m_PostData.Rows[i][j].ToString();

                    }
                    sw.WriteLine(tempStr);
                    tempStr = "";

                    m_Sender.BeginInvoke(m_SenderDelegate, new object[] { Count, i, false });
                }

                sw.Close();
                myStream.Close();

            }
            catch(Exception)
            {
                return;
            }
            finally
            {
                sw.Close();
                myStream.Close();

                m_Sender.BeginInvoke(m_SenderDelegate, new object[] { Count, i, true });
            }

            return;
        }
        #endregion

        #region ExportText
        /// <summary>
        /// export to text
        /// </summary>
        private void ExportText()
        {
            FileStream myStream = File.Open(m_FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            string str = "";
            string tempStr = "";
            int i = 0;
            int Count = 0;

            try
            {
                for(i = 0; i < m_PostData.Columns.Count; i++)
                {
                    str += "\t";
                    str += m_PostData.Columns[i].ColumnName;
                }

                sw.WriteLine(str);

                Count = m_PostData.Rows.Count;
                for(i = 0; i < m_PostData.Rows.Count; i++)
                {
                    for(int j = 0; j < m_PostData.Columns.Count; j++)
                    {

                        tempStr += "\t";
                        tempStr += m_PostData.Rows[i][j];
                    }
                    sw.WriteLine(tempStr);
                    tempStr = "";

                    m_Sender.BeginInvoke(m_SenderDelegate, new object[] { Count, i, false });
                }

                sw.Close();
                myStream.Close();

            }
            catch(Exception)
            {
                return;
            }
            finally
            {
                sw.Close();
                myStream.Close();

                m_Sender.BeginInvoke(m_SenderDelegate, new object[] { Count, i, true });
            }

            return;
        }
        #endregion
    }
}
